package com.mpsc.fbpm.consumer.util;

import com.google.common.collect.Lists;
import com.mpsc.fbpm.consumer.requestBean.ProcessApproveQuery;
import com.mpsc.fbpm.consumer.requestBean.ProcessBackQuery;
import com.mpsc.fbpm.consumer.requestBean.ProcessCancelQuery;
import com.mpsc.fbpm.consumer.requestBean.ProcessStartQuery;
import com.mpsc.fbpm.consumer.requestBean.variable.ProcessInstanceCreateRequest;
import com.mpsc.fbpm.consumer.requestBean.variable.RestVariable;
import com.mpsc.fbpm.consumer.requestBean.variable.TaskFormRequest;

import java.util.List;

public class FbpmUtil {

    /**
     * 批量组装工作流启动参数
     *
     * @param billList    单据Id
     * @param rgCode      区划
     * @param bizTypeCode 单据类型
     * @param userCode    用户
     * @param variables   网关参数
     */
    public static ProcessStartQuery getProcessStartQuery(List<String> billList, String bizTypeCode, String rgCode, String userCode, List<RestVariable> variables) {
        ProcessStartQuery startQuery = new ProcessStartQuery();
        List<ProcessInstanceCreateRequest> batchProcessInstanceCreateRequest = Lists.newArrayList();
        if (null != billList) {
            ProcessInstanceCreateRequest request;
            for (String bill : billList) {
                request = new ProcessInstanceCreateRequest();
                request.setBizTypeCode(bizTypeCode);
                request.setBusinessKey(bill);
                request.setRgCode(rgCode);
                if (null != variables) {
                    request.setVariables(variables);
                }
                batchProcessInstanceCreateRequest.add(request);
            }

        }
        startQuery.setUserCode(userCode);
        startQuery.setBatchProcessInstanceCreateRequest(batchProcessInstanceCreateRequest);
        return startQuery;
    }

    /**
     * 批量组装工作流审核参数
     *
     * @param taskIdList 任务id
     * @param userCode   用户
     * @param variables  网关参数
     */
    public static ProcessApproveQuery getProcessApproveQuery(List<String> taskIdList, String userCode, List<RestVariable> variables) {
        ProcessApproveQuery approveQuery = new ProcessApproveQuery();
        List<TaskFormRequest> taskFormRequests = Lists.newArrayList();
        if (null != taskIdList) {
            TaskFormRequest request;
            for (String taskId : taskIdList) {
                request = new TaskFormRequest();
                request.setTaskId(taskId);
                if (null != variables) {
                    request.setVariables(variables);
                }
                taskFormRequests.add(request);
            }

        }
        approveQuery.setUserCode(userCode);
        approveQuery.setTaskIds(String.join(",", taskIdList));
        approveQuery.setTaskFormRequests(taskFormRequests);
        return approveQuery;
    }

    /**
     * 批量组装工作流撤销参数
     *
     * @param taskIdList 任务id
     * @param userCode   用户
     * @param variables  网关参数
     */
    public static ProcessCancelQuery getProcessCancelQuery(List<String> taskIdList, String userCode, List<RestVariable> variables) {
        ProcessCancelQuery cancelQuery = new ProcessCancelQuery();
        List<TaskFormRequest> taskFormRequests = Lists.newArrayList();
        if (null != taskIdList) {
            TaskFormRequest request;
            for (String taskId : taskIdList) {
                request = new TaskFormRequest();
                request.setTaskId(taskId);
                if (null != variables) {
                    request.setVariables(variables);
                }
                taskFormRequests.add(request);
            }

        }
        cancelQuery.setUserCode(userCode);
        cancelQuery.setTaskIds(String.join(",", taskIdList));
        cancelQuery.setTaskFormRequests(taskFormRequests);
        return cancelQuery;
    }

    /**
     * 批量组装工作流驳回参数
     *
     * @param taskIdList 任务id
     * @param userCode   用户
     * @param variables  网关参数
     */
    public static ProcessBackQuery getProcessBackQuery(List<String> taskIdList, String userCode, List<RestVariable> variables) {
        ProcessBackQuery backQuery = new ProcessBackQuery();
        List<TaskFormRequest> taskFormRequests = Lists.newArrayList();
        if (null != taskIdList) {
            TaskFormRequest request;
            for (String taskId : taskIdList) {
                request = new TaskFormRequest();
                request.setTaskId(taskId);
                if (null != variables) {
                    request.setVariables(variables);
                }
                taskFormRequests.add(request);
            }
        }
        backQuery.setUserCode(userCode);
        backQuery.setTaskIds(String.join(",", taskIdList));
        backQuery.setTaskFormRequests(taskFormRequests);
        return backQuery;
    }

}
